package com.huatai.datadevelop.thread.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huatai.common.api.R;
import com.huatai.datadevelop.entity.RealApiManageEntity;
import com.huatai.datadevelop.mapper.ApiSubscreptionMapper;
import com.huatai.datadevelop.service.RealApiManageService;
import com.huatai.datadevelop.websocket.TimingApiWebSocket;
import com.huatai.datadevelop.websocket.TimingTableWebSocket;
import com.huatai.datadevelop.websocket.TimingWebSocket;
import com.huatai.datadevelop.websocket.pageAnd;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import com.huatai.datadevelop.constant.Constant;
import com.huatai.datadevelop.entity.ApiSuperviseEntity;
import com.huatai.datadevelop.entity.TableManageEntity;
import com.huatai.datadevelop.service.ApiSuperviseService;
import com.huatai.datadevelop.service.TemplateService;
import com.huatai.datadevelop.service.TokenConfigService;
import com.huatai.datadevelop.thread.service.InsertDataBaseExecutor;
import com.huatai.datadevelop.utils.InitHashMapUtil;
import com.huatai.datadevelop.utils.JDBCUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hecai
 * @description: TODO
 * @date 2023/2/16 10:29
 * @Version 1.0
 */
@Slf4j
@Component("TimingApiTask")
public class TimingApiTask {

    @Autowired
    private RealApiManageService realApiManageService;
    @Autowired
    private TimingApiWebSocket timingApiWebSocket;
    @Autowired
    private TimingWebSocket timingWebSocket;
    @Autowired
    private TimingTableWebSocket timingTableWebSocket;
    @Autowired
    private ApiSubscreptionMapper apiSubscreptionMapper;

    /**
     * 定时调用接口数据，推送给前端
     */
	@XxlJob("timingApiTaskRunTask")
    public void runTask() throws IOException {
		String param = XxlJobHelper.getJobParam();
		this.sendMessage(param);
    }

    /**
     * 异步推送处理数据
     * @param param
     */
    public void sendMessage(String param) throws IOException {
        JSONObject jsonParam = JSON.parseObject(param);
        String interfaceName = jsonParam.getString("interfaceName");
        RealApiManageEntity entityByInterfaceName = this.realApiManageService.getEntityByInterfaceName(interfaceName);
        JSONObject data = jsonParam.getJSONObject("data");
        Object objectData = this.realApiManageService.asyncRequestApi(data, entityByInterfaceName);
        R pushData = R.data(objectData);
        timingApiWebSocket.sendMessage(interfaceName,JSON.toJSONString(pushData));
    }

    /**
     * 定时调用表格数据，推送给前端
     */
	@XxlJob("timingApiTaskRunTableTask")
    public void runTableTask() throws IOException {
		String param = XxlJobHelper.getJobParam();
		JSONObject jsonParam = JSON.parseObject(param).getJSONObject("data");
        String interfaceName = jsonParam.getString("interfaceName");
        Integer totalPage = jsonParam.getInteger("totalPage");
        Integer limit = jsonParam.getInteger("limit");
        String sqlStr = jsonParam.getString("sqlStr");
        pageAnd.resSet(interfaceName);
        Integer page = pageAnd.getPage();
        if(page!=null && page<=totalPage){
            pageAnd.resSet(interfaceName);
        }
        List<JSONObject> tableDataList = this.getTableDataList(sqlStr,page,limit);
        timingTableWebSocket.sendMessage(interfaceName,JSON.toJSONString(tableDataList));
        pageAnd.addNumber();
    }

    /**
     * 获取数据库表数据
     * @param sqlStr
     * @param pageIndex
     * @param limit
     * @return
     */
    public List<JSONObject> getTableDataList(String sqlStr,int pageIndex,int limit){
        List<JSONObject> dataList = new ArrayList<>();
        String limitStr =  " limit " + (pageIndex - 1) * limit +" , " + limit + ";";
        dataList = this.apiSubscreptionMapper.getTableDataList(sqlStr,limitStr);
        return dataList;
    }

    /**
     * 异步推送处理数据
     * @param param
     */
    public void sendPageMessage(String param) throws IOException {
        Integer page = pageAnd.getPage();
        timingWebSocket.sendMessage(param,page,6400);
        pageAnd.addNumber();
    }
}
